﻿using AllProAutoUpData.Server;
using FD_DB;
using System.Text;

namespace AllProAutoUpData
{
    public partial class MainPage : ContentPage
    {
        UpServer Server;
        FileDB fileDB;

        Task UpUi;
        public MainPage()
        {
            InitializeComponent();
            Server = new UpServer();
            fileDB = new FileDB();
        }

        private void ContentPage_Loaded(object sender, EventArgs e)
        {
            Server.Start(fileDB.Model.Port);
            Server.MessReceive += Server_MessReceive;
            lbEdition.BindingContext = fileDB.Model;
            lbEdition.SetBinding(Label.TextProperty, "Edition");
            lbEditionTime.Text = fileDB.Model.EditionTime.ToString();
            UpUi = Task.Run(UpUI);
        }
        /// <summary>
        /// 消息到达
        /// </summary>
        /// <param name="data"></param>
        /// <param name="conn"></param>
        /// <exception cref="NotImplementedException"></exception>
        private void Server_MessReceive(byte[] data, IntPtr conn)
        {
            try
            {
                var rstr = Encoding.UTF8.GetString(data);
                var jobj = System.Text.Json.JsonSerializer.Deserialize<MessageModeClient>(rstr);
                MessageMode mode;
                if (jobj.rtype == 0)
                {
                    mode = new()
                    {
                        Edition = fileDB.Model.Edition,
                        FtpUser= fileDB.Model.FtpUser,
                        FtpPass= fileDB.Model.FtpPass,
                    };
                    Server.Send(conn, Encoding.UTF8.GetBytes(System.Text.Json.JsonSerializer.Serialize(mode)));
                }
            }
            catch
            {

            }
        }
        /// <summary>
        /// 文件转字节
        /// </summary>
        /// <param name="path"></param>
        /// <returns></returns>
        private byte[] FileToByte(string path)
        {
            using (FileStream fs=new FileStream(path,FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.ReadWrite))
            {
                byte[] bytes = new byte[fs.Length];
                fs.Read(bytes, 0, bytes.Length);
                return bytes;
            }
        }
        private void UpUI()
        {
            while (true)
            {
                MainThread.BeginInvokeOnMainThread(new Action(() =>
                {
                    slLinks.Clear();
                    if (Server.Clients.Count == 0)
                    {
                        slLinks.Add(new Label() { Text = "无链接" });
                    }
                    foreach (var item in Server.Clients)
                    {
                        slLinks.Add(new Label() { Text = item.Key });
                    }

                    string mess = "";
                    foreach (var item in Server.Message)
                    {
                        mess += item + "\r\n";
                    }
                    if (Server.Message.Count == 0)
                    {
                        mess += "无消息";
                    }
                    eMessage.Text = mess;
                }));
                Thread.Sleep(1000);
            }
        }
        /// <summary>
        /// 发布新版本
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btRelease_Clicked(object sender, EventArgs e)
        {
            fileDB.Model.Edition++;
            fileDB.Model.EditionTime = DateTime.Now;
            fileDB.Updata();
            lbEdition.SetBinding(Label.TextProperty, "Edition");
            lbEditionTime.Text = fileDB.Model.EditionTime.ToString();
        }
        /// <summary>
        /// 选择文件夹
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void btFilePath_Clicked(object sender, EventArgs e)
        {
            //var files = Directory.GetFiles(eyPath.Text);
            //slFiles.Clear();
            //for (int i = 0; i < files.Length; i++)
            //{
            //    Label lb = new Label();
            //    lb.Text = files[i];
            //    lb.Padding = new Thickness(3, 3, 3, 3);
            //    slFiles.Add(lb);
            //}
            //var h = slFiles.GetValue(StackLayout.HeightProperty);
            //slFiles.Parent.Parent.SetValue(StackLayout.HeightProperty,h);
        }
    }
}